<? 
function hide_picture($picture_ID, $main)
{
	$sql="UPDATE `gallery` SET `active` = 0 WHERE `ID` = $picture_ID";
	mysql_query($sql) or die("Cannot hide picture<BR>" . $sql);
	if ($main != 0)
	{
		$sql2 = "UPDATE `events` SET `main_image_ID` = NULL WHERE `ID` = $main";
		mysql_query($sql2) or die("Cannot hide picture for event<BR>" . $sql2);
	}
}
function show_picture($picture_ID)
{
	$sql="UPDATE `gallery` SET `active` = 1 WHERE `ID` = $picture_ID";
	mysql_query($sql) or die("Cannot show picture<BR>" . $sql);
}
function make_main_image($picture_ID,$event_ID)
{
	$sql="UPDATE `events` SET `main_image_ID` = $picture_ID WHERE `ID` = $event_ID";
	mysql_query($sql) or die("Cannot make picture main for event<BR>" . $sql);
}
function save_info($picture_ID)
{
	$title = $_POST['title'];
	$audio_description = $_POST['audio_description'];
	$sql="UPDATE `gallery` SET `title` = '$title' , `audio_description` = '$audio_description' WHERE `ID` = $picture_ID";
	mysql_query($sql) or die("Cannot update picture info<BR>" . $sql);
}
function get_main_image_ID($event_ID)
{
	$sql="SELECT `main_image_ID` from `events` where `ID` = $event_ID";
	$result = mysql_query($sql) or die("Cannot get main image for event <BR>" . $sql);
	$id = mysql_fetch_assoc($result);
	mysql_free_result($result);
	return $id['main_image_ID'];
}
function print_all_images_for_event($event_ID)
{
	$sql = "SELECT * FROM `gallery` WHERE `event_ID` = $event_ID";
	$result = mysql_query($sql) or die("Cannot get pictures for event " . $sql);
	$main_image_ID = get_main_image_ID($event_ID);
	for ($i=0;$i<mysql_num_rows($result);$i++)
	{
		$image_details = mysql_fetch_assoc($result); 
		echo "<A href='gallery_edit.php?realm=events&picture=" . $image_details['ID'] . "'>Edit info for " . $image_details['title'] . "</A><BR>Picture is ";
		if ($image_details['active'] == 1) 
		{
			echo " visible <A href='gallery_admin.php?realm=events&action=hide&picture={$image_details['ID']}&main=";
			if (($image_details['ID'] == $main_image_ID)) echo "$event_ID"; 
				else echo "0"; 
			echo "'>Hide</A><BR>";
		}
		else echo " hidden <A href='gallery_admin.php?realm=events&action=show&picture={$image_details['ID']}'>Show</A><BR>";
		if ($image_details['ID'] == $main_image_ID) echo "This is main picture for event";
		else echo "<A href='gallery_admin.php?realm=events&action=make_main&event=$event_ID&picture={$image_details['ID']}'>Make main picutre for event</A>";
		?>
		<BR>
		<img src="../images/<? echo $image_details['image_thumbnail'];?>" alt="<? echo $image_details['audio_description'];?>" />
		<BR>
		<?
		//echo "<a class='thumbnail' href='#thumb'><img src='images/{$image_details['image_thumbnail']}' width='8%' height='10%' border='0' /><span><img src='images/{$image_details['image']}' /><br />{$image_details['audio_description']}</span></a>";	
	}
	mysql_free_result($result);
}

function print_all_events()
{
	$sql="SELECT * FROM `events` WHERE 1 ORDER BY `events`.`ID` ASC";
	$result = mysql_query($sql) or die (mysql_error());
	for ($i=0;$i<mysql_num_rows($result);$i++)
	{
		$row = mysql_fetch_assoc($result); 
		$ID = $row['ID'];
		if (! empty($row['date']))
				$Date = format_and_extend_date($row['date']);
		else
				$Date = 'Planned';
		$Summary = $row['summary'];
		?>
		<H2 class="event_title"> <A href="gallery_upload.php?ID=<? echo $ID; ?>&realm=events">Upload image for event:"<? echo $Summary; ?>"</A></H2><BR>
		<?print_all_images_for_event($ID);?>
		<BR /><BR />
		<?
	}
	mysql_free_result($result);
}
?>
